/**
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-01-20 10:56:26
 * @Description  : 该工具类用于根据实体的ID获取部分属性
 */

const { Department, Permission, Role } = require('@/models/model')
const { Op } = require('sequelize')

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 根据ids获取部门信息
 * @param {*} ids
 */
module.exports.getDepByIds = async (ids) => {
  let deps = []
  for (let i = 0; i < ids.length; i++) {
    if (+ids[i] === -1) {
      deps.push({
        id: '-1',
        name: '根部门'
      })
      continue
    }
    const dep = await Department.findByPk(BigInt(ids[i]), {
      attributes: ['id', 'name']
    })
    deps.push({
      id: dep.id,
      name: dep.name
    })
  }
  return deps
}

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 根据协作ID数组获取协作部门信息
 * @param {*} ids
 */
module.exports.getCopDepByIds = async (ids) => {
  let deps = []
  for (let i = 0; i < ids.length; i++) {
    if (+ids[i] === 0) {
      deps.push({
        id: '0',
        name: '所有部门'
      })
      continue
    }
    if (+ids[i] === -1) {
      deps.push({
        id: '1',
        name: '当前部门及其子部门'
      })
      continue
    }
    const dep = await Department.findByPk(BigInt(ids[i]), {
      attributes: ['id', 'name']
    })
    deps.push({
      id: dep.id,
      name: dep.name
    })
  }
  return deps
}

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 根据权限ID获取权限详情
 * @param {*} ids
 */
module.exports.getPermissionByIds = async (ids) => {
  let permissions = []
  for (let i = 0; i < ids.length; i++) {
    const permission = await Permission.findByPk(BigInt(ids[i]), {
      attributes: ['id', 'name', 'ename']
    })
    permissions.push({
      id: permission.id,
      name: permission.name,
      ename: permission.ename
    })
  }
  return permissions
}

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 根据user-dep对象获取对应user，dep，sizeMax情况
 * @param {*} departments user-dep数组情况
 */
module.exports.getUserDepByDepartments = async (departments) => {
  // 查询出角色所处部门的详情
  const userDepInfo = {
    depIds: [],
    roleIds: [],
    sizeMaxs: []
  }
  for (let i = 0; i < departments.length; i++) {
    const curUserDep = departments[i]
    userDepInfo.depIds.push(BigInt(curUserDep.departmentId))
    userDepInfo.roleIds.push(BigInt(curUserDep.roleId))
    userDepInfo.sizeMaxs.push(curUserDep.sizeMax)
  }

  const deps = await Department.findAll({
    where: {
      id: {
        [Op.in]: userDepInfo.depIds
      }
    },
    attributes: ['id', 'name']
  })

  const roles = await Role.findAll({
    where: {
      id: {
        [Op.in]: userDepInfo.roleIds
      }
    },
    attributes: ['id', 'name']
  })

  const userDeps = []
  for (let i = 0; i < departments.length; i++) {
    userDeps.push({
      dep: { id: deps[i].id, name: deps[i].name },
      role: {
        id: departments[i].roleId,
        name: roles.find((item) => item.id === departments[i].roleId).name
      },
      sizeMax: userDepInfo.sizeMaxs[i]
    })
  }

  return userDeps
}
